package com.yhy.wxzn.wcs.utils;

import com.yhy.wxzn.wcs.enums.LifterLocation;

import java.util.BitSet;
import java.util.Date;

public class PlcUtils {

    public static long getTimeDifference(Date startDate) {
        Date endDate = new Date();
        long different = endDate.getTime() - startDate.getTime();
        long value = 600 - different;
        return value > 0 ? value : 10;
    }

    /**
     * 输送线组byte
     *
     * @param branchNo
     * @param type
     * @param mcsId
     * @return
     */
    public static byte[] taskTransmissionByte(int branchNo, int type, int mcsId) {
        byte[] bytes = new byte[8];
        //目标站
        bytes[0] = (byte) ((branchNo >> 8) & 0xFF);
        bytes[1] = (byte) ((branchNo) & 0xFF);
        //任务类型
        bytes[2] = (byte) ((type >> 8) & 0xFF);
        bytes[3] = (byte) ((type) & 0xFF);
        //任务号
        bytes[4] = (byte) ((mcsId >> 24) & 0xFF);
        bytes[5] = (byte) ((mcsId >> 16) & 0xFF);
        bytes[6] = (byte) ((mcsId >> 8) & 0xFF);
        bytes[7] = (byte) (mcsId & 0xFF);

        return bytes;
    }




    public static byte[] taskHoistByte(int sourceFloor, LifterLocation sourceLifterLocation,
                                       int targetFloor,LifterLocation targetLifterLocation,
                                       int type, int lifterPlcTaskId) {


        byte[] bytes = new byte[14];
        //提升机起始层
        bytes[0] = (byte) ((sourceFloor >> 8) & 0xFF);
        bytes[1] = (byte) ((sourceFloor) & 0xFF);
        //提升机起始工位
        bytes[2] = (byte) ((sourceLifterLocation.getCode() >> 8) & 0xFF);
        bytes[3] = (byte) ((sourceLifterLocation.getCode()) & 0xFF);
        //提升机目标层
        bytes[4] = (byte) ((targetFloor >> 8) & 0xFF);
        bytes[5] = (byte) ((targetFloor) & 0xFF);
        //提升机目标工位
        bytes[6] = (byte) ((targetLifterLocation.getCode() >> 8) & 0xFF);
        bytes[7] = (byte) ((targetLifterLocation.getCode()) & 0xFF);
        //任务类型
        bytes[8] = (byte) ((type >> 8) & 0xFF);
        bytes[9] = (byte) ((type) & 0xFF);
        //任务号
        bytes[10] = (byte) ((lifterPlcTaskId >> 8) & 0xFF);
        bytes[11] = (byte) (lifterPlcTaskId & 0xFF);
        bytes[12] = (byte) ((lifterPlcTaskId >> 24) & 0xFF);
        bytes[13] = (byte) ((lifterPlcTaskId >> 16) & 0xFF);

        return bytes;
    }

    public static byte[] writeBool(boolean a, boolean b, boolean c, boolean d) {
        byte[] bytes = new byte[1];
        BitSet bitSet = BitSet.valueOf(bytes);
        bitSet.set(0, a);
        bitSet.set(1, b);
        bitSet.set(2, c);
        bitSet.set(3, d);

        return bitSet.toByteArray();
    }
}
